<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


class Sms
{       
   
        /**
         * 模拟post进行url请求
         * @param string $url
         * @param array $post_data
         */
    private function request_post($url = '', $post_data = array()) {
        if (empty($url) || empty($post_data)) {
            return false;
        }

        $o = "";
        foreach ( $post_data as $k => $v ) 
        { 
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);

        $postUrl = $url;
        $curlPost = $post_data;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;
    }


    // XML格式转数组格式
    private function xml_to_array( $xml ) { 
        $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/"; 
        if(preg_match_all($reg, $xml, $matches)) { 
            $count = count($matches[0]); 
            for($i = 0; $i < $count; $i++) { 
                $subxml= $matches[2][$i]; 
                $key = $matches[1][$i]; 
                if(preg_match( $reg, $subxml )) { 
                    $arr[$key] = self::xml_to_array( $subxml ); 
                } else {
                    $arr[$key] = $subxml; 
                } 
            } 
        } 
        return $arr; 
    } 

    // 页面显示数组格式，用于调试
    private function echo_xmlarr($res) {
        $res = $this->xml_to_array($res);
        echo "<pre>";
        print_r($res);
        echo "</pre>";
    }


    /**
         * 短信调用
         * @param array $data
         * @param int   $ser_var 服务器账号类别 1.恒泰 2.按揭无忧
         * @return none 
         *  $data['method'] = 'sendMsg';
            $data['mobile']  电话号码;
            $data['content'] 普通短信为短信内容,模板短信为参数值多个用逗号隔开;
            $data['msgtype']= '1';       // 1-普通短信，2-模板短信
         *  $data['tempid'] = 'JSM41451-0002'; // 模板编号
            $data['code']   = 'utf-8';   // utf-8,gbk
         */
    public function send_sms($data ,$ser_var = 1) {
        $now = gmdate("D, d M Y H:i:s") . " GMT";
        header("Date: $now");
        header("Expires: $now");
        header("Last-Modified: $now");
        header("Pragma: no-cache");
        header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");
        //服务器参数设置
        $svr_url = 'http://112.74.76.186:8030/service/httpService/httpInterface.do';   // 服务器接口路径
        $svr_param = array();
        if ($ser_var == 2) {
            $svr_param['username'] = 'JSM41451';
        } else {
            $svr_param['username'] = 'JSM4145101';    // 账号 
        }
        $svr_param['password'] = 'Cpic1234';  // 密码
        $svr_param['veryCode'] = 'lp8jdm6a2ya7';

        $post_data = array();
        $post_data = array_merge($svr_param,$data);
        $res = $this->request_post($svr_url, $post_data);  // 如果账号开了免审，或者是做模板短信，将会按照规则正常发出，而不会进人工审核平台
        $res = $this->xml_to_array($res);
        return $res;
    }
}